1599C - Bubble Strike - CodeForces Solution


combinatorics math probabilities ternary search *2000

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using indexed_set = tree<T, null_type, less<T>, 
            rb_tree_tag, tree_order_statistics_node_update>;
 
#define ll long long
#define ld long double
#define cy cout<<"YES\n"
#define cn cout<<"NO\n"
 
bool sortbysec(const pair<ll,ll> &a,const pair<ll,ll> &b)
{ return (a.second < b.second); }
 
ll fastpow(ll a, ll b, ll m)
{
    if(b==0)return 1;
    if(b%2==0){ll take=fastpow(a,b/2,m);return (take*take)%m;}
    else{ll take=fastpow(a,b-1,m);return (a*take)%m;}
}

void modularInverse(ll n, ll prime, vector<ll> &dp)
{
    dp[0] = dp[1] = 1;
    for (ll i = 2; i <= n; i++)
        dp[i] = dp[prime % i] *
               (prime - prime / i) % prime;   
}

ll ncr(vector<ll> &arr, ll a, ll b, ll mod)
{
    ll ans=(arr[a]*fastpow(arr[b],mod-2,mod))%mod;
    ans=(ans*fastpow(arr[a-b],mod-2,mod))%mod;
    return ans;
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    // freopen("haybales.in", "r", stdin);
    // freopen("haybales.out", "w", stdout);
    int cases=1;
    // cin>>cases;
    ll mod=1e9+7;
    // Error can be because you didn't input the entreies
    while(cases--)
    {
        ll n;cin>>n;
        double p;cin>>p;
        vector<ll> arr(n+1,1);
        for(ll i=1;i<=n;i++)arr[i]=(arr[i-1]*i);
        ll ans;
        for(ll i=0;i<=n;i++)
        {   
            ll x0=(((n-i)*(n-i-1)*(n-i-2))/6);
            ll x1=i*(((n-i)*(n-i-1))/2);
            ll x2=((i*(i-1))/2)*(n-i);
            ll x3=(((i)*(i-1)*(i-2))/6);
            double z=1.0;
            double it=((2*x2+2*x3+x1)*z)/(2*(x0+x1+x2+x3));
            if(it>=p)
            {
                ans=i;
                break;
            }
        }   
        cout<<ans;
    }
    
    // minimise distance in a matkjrix, make a queue with the source and check for adjacent cells
    // max/min problem ?? try to solve using binary search
    // find result in form of mod ?? never use divide operation
    
    return 0;
}















Comments

Submit
0 Comments
More Questions

71. Simplify Path
62. Unique Paths
50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices